using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Notifications;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using AzrngTools.ViewModels;
using AzrngTools.ViewModels.Encode;
using AzrngTools.ViewModels.Encrypts;
using AzrngTools.ViewModels.Format;
using AzrngTools.ViewModels.Other;
using AzrngTools.ViewModels.Setting;
using AzrngTools.ViewModels.TextHandle;
using AzrngTools.Views;
using AzrngTools.Views.Encode;
using AzrngTools.Views.Encrypts;
using AzrngTools.Views.Format;
using AzrngTools.Views.Other;
using AzrngTools.Views.Setting;
using AzrngTools.Views.TextHandle;
using GTranslate.Translators;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace AzrngTools;

public partial class App : Application
{
    public App()
    {
        Services = ConfigureServices();
    }

    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);

        ViewLocator.Register<MainWindowViewModel, MainWindow>();
        ViewLocator.Register<OverviewPageViewModel, OverviewPageView>();

        ViewLocator.Register<HashPageViewModel, HashPageView>();
        ViewLocator.Register<AesEncryptPageViewModel, AesEncryptPageView>();
        ViewLocator.Register<DesEncryptPageViewModel, DesEncryptPageView>();
        ViewLocator.Register<HmacHashPageViewModel, HmacHashPageView>();
        ViewLocator.Register<Sm4EncryptPageViewModel, Sm4EncryptPageView>();
        ViewLocator.Register<RsaEncryptPageViewModel, RsaEncryptPageView>();

        ViewLocator.Register<GuidShowPageViewModel, GuidShowPageView>();
        ViewLocator.Register<Base64EncodePageViewModel, Base64EncodePageView>();
        ViewLocator.Register<TranslatorPageViewModel, TranslatorPageView>();
        ViewLocator.Register<TodayNewsPageViewModel, TodayNewsPageView>();

        ViewLocator.Register<JsonPageViewModel, JsonPageView>();
        ViewLocator.Register<SqlFormatPageViewModel, SqlFormatPageView>();
        ViewLocator.Register<XmlToHtmlPageViewModel, XmlToHtmlPageView>();
        ViewLocator.Register<JwtEncodePageViewModel, JwtEncodePageView>();
        ViewLocator.Register<RegexAnalysisViewModel, RegexAnalysisView>();

        //ViewLocator.Register<IcoToConvertPageViewModel, IcoToConvertPageView>();
        ViewLocator.Register<GzipEncodePageViewModel, GzipEncodePageView>();
        ViewLocator.Register<EncodePageViewModel, EncodePageView>();
        ViewLocator.Register<MarkdownPageViewModel, MarkdownPageView>();
        ViewLocator.Register<JsonSchemaPageViewModel, JsonSchemaPageView>();
        ViewLocator.Register<AboutPageViewModel, AboutPageView>();
        ViewLocator.Register<StringPageViewModel, StringPageView>();
        ViewLocator.Register<HardwarePageViewModel, HardwarePageView>();
    }

    public override void OnFrameworkInitializationCompleted()
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            // 下面的行需要删除Avalonia数据验证，如果没有这一行，您将获得来自Avalonia和CT的重复验证
            BindingPlugins.DataValidators.RemoveAt(0);
            desktop.MainWindow = Services.GetRequiredService<MainWindow>();
        }

        base.OnFrameworkInitializationCompleted();
    }

    /// <summary>
    /// Gets the current <see cref="App"/> instance in use
    /// </summary>
    public static new App Current => (App)Avalonia.Application.Current;

    /// <summary>
    /// 通知页面
    /// </summary>
    public static WindowNotificationManager NotificationPage { get; set; }

    /// <summary>
    /// Gets the <see cref="IServiceProvider"/> instance to resolve application services.
    /// </summary>
    public IServiceProvider Services { get; }

    /// <summary>
    /// Configures the services for the application.
    /// </summary>
    private static IServiceProvider ConfigureServices()
    {
        var services = new ServiceCollection();
        services.AddLogging(builder => { builder.AddConsole(); });

        services.AddSingleton<MainWindow>();
        services.AddSingleton<ITranslator, YandexTranslator>();
        services.AddHttpClient();

        var assembly = AssemblyHelper.GetEntryAssembly();
        services.RegisterBusinessServices(assembly);

        // 注入ViewModels
        services.AddTransient<MainWindowViewModel>();
        services.AddTransient<OverviewPageViewModel>();

        services.AddTransient<HashPageViewModel>();
        services.AddTransient<AesEncryptPageViewModel>();
        services.AddTransient<DesEncryptPageViewModel>();
        services.AddTransient<HmacHashPageViewModel>();
        services.AddTransient<Sm4EncryptPageViewModel>();
        services.AddTransient<RsaEncryptPageViewModel>();

        services.AddTransient<GuidShowPageViewModel>();
        services.AddTransient<JsonSchemaPageViewModel>();
        services.AddTransient<Base64EncodePageViewModel>();
        services.AddTransient<TranslatorPageViewModel>();
        services.AddTransient<TodayNewsPageViewModel>();

        services.AddTransient<StringPageViewModel>();
        services.AddTransient<JsonPageViewModel>();
        services.AddTransient<SqlFormatPageViewModel>();
        services.AddTransient<XmlToHtmlPageViewModel>();
        services.AddTransient<JwtEncodePageViewModel>();
        services.AddTransient<RegexAnalysisViewModel>();

        // services.AddTransient<IcoToConvertPageViewModel>();
        services.AddTransient<GzipEncodePageViewModel>();
        services.AddTransient<EncodePageViewModel>();
        services.AddTransient<MarkdownPageViewModel>();
        services.AddTransient<AboutPageViewModel>();
        services.AddTransient<HardwarePageViewModel>();

        return services.BuildServiceProvider();
    }
}